package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.impl.reportTemp;

import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.IntDataSrcItemValueMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.JsmReportMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.ReportTotalValueMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.IntDataSrcItemValue;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Report;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.ReportTotalValue;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.reportTemp.JsmReportService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 修改报表各种信息service实现类
 * Created by yangcs on 2017/3/20.
 */

public class JsmReportServiceImpl implements JsmReportService {

    @Autowired
    private JsmReportMapper jsmReportMapper;
    @Autowired
    private IntDataSrcItemValueMapper intDataSrcItemValueMapper;
    @Autowired
    private ReportTotalValueMapper reportTotalValueMapper;

    /**
     * 修改报表信息
     * @param report
     * @return
     */
    @Override
    public int saveInfo(Report report) {
        return jsmReportMapper.updateByPrimaryKey(report);
    }

    /**
     * 修改整点数据
     * @param value
     * @return
     */
    @Override
    public HashMap saveValue(IntDataSrcItemValue value) {
        HashMap map = new HashMap<>();
        int result;
        IntDataSrcItemValue localValue = intDataSrcItemValueMapper.queryValue(value);
        // 设置时间戳
        value.setTimestamp(DateUtil.DateToString(new Date(),"MM/dd/yy HH:mm:ss"));
        // 缺失整点数据 插入
        if( localValue == null ){
            // 插入之前先查询C 如果存在先返回数据，如果不存在直接插入
            result = intDataSrcItemValueMapper.insertValue(value);
        } else {
            // 存在整点数据，修改
            result = intDataSrcItemValueMapper.updateValue(value);
        }
        map.put("result",result);
        return map;
    }

    /**
     * 修改合计数据
     * @param reportTotalValue
     * @return
     */
    @Override
    public int saveSum(ReportTotalValue reportTotalValue,String userId) {
        int result;
        ReportTotalValue localValue = reportTotalValueMapper.queryValue(reportTotalValue);
        // 缺失数据 插入
        if( localValue == null ){
            reportTotalValue.setValueId(UniqueUtil.uuid());
            reportTotalValue.setCreateUserId(userId);
            reportTotalValue.setModifyUserId(userId);
            result = reportTotalValueMapper.insert(reportTotalValue);
        } else {
            // 存在数据，修改
            result = reportTotalValueMapper.updateValue(reportTotalValue);
        }
        return result;
    }

    /**
     * @param intDataSrcItemValue
     */
    @Override
    public List<IntDataSrcItemValue> selectIntDataSrcItemList(IntDataSrcItemValue intDataSrcItemValue) {
        return intDataSrcItemValueMapper.selectIntDataSrcItemList(intDataSrcItemValue);
    }

    @Override
    public IntDataSrcItemValue getLastIntDataSrcItemValue(IntDataSrcItemValue intDataSrcItemValue) {
        IntDataSrcItemValue isv = null ;
        List<IntDataSrcItemValue> intDataSrcItemValues = intDataSrcItemValueMapper.getLastIntDataSrcItemValue(intDataSrcItemValue);
        if(intDataSrcItemValues != null && intDataSrcItemValues.size() > 0){
            isv = intDataSrcItemValues.get(0);
        }
        return isv;
    }
}
